﻿using PureMVC.Interfaces;
using PureMVC.Patterns;
using System;
using System.IO;
using System.Reflection;
using System.Windows.Forms;
using Rail.Start.Controller;
using Rail.Base.Settings;

namespace Rail.Start
{
    public class ApplicationFacade : Facade
    {
        public const string STARTUP = "startup";
        public new static IFacade Instance
        {
            get
            {
                if (m_instance == null)
                {
                    lock (m_staticSyncRoot)
                    {
                        if (m_instance == null) m_instance = new ApplicationFacade();
                    }
                }

                return m_instance;
            }
        }
        /// <summary>
        /// 初始化注册command，start库中的使用RegisterCommand注册、其余动态库中的通过RegisterSampleCommand方法动态注册；
        /// </summary>
        protected override void InitializeController()
        {
            base.InitializeController();
            RegisterCommand(STARTUP, typeof(RuninitCommand));
            RegisterCommand("MenuSettingCommand", typeof(MenuSettingCommand));
            RegisterCommand("CreateDemoFormCommand", typeof(CreateDemoFormCommand));
            RegisterCommand("ProjectFoundFormCommand", typeof(ProjectFoundFormCommand));
            RegisterCommand("NetWorkSettingCommand", typeof(NetWorkSettingCommand));
            RegisterCommand("OpenEngineeringFormCommand", typeof(OpenEngineeringFormCommand));
            RegisterCommand("SaveAsEngineeringFormCommand", typeof(SaveAsEngineeringFormCommand));
            RegisterSampleCommand();
        }
        void RegisterSampleCommand()
        {
            SplashForm sf = new SplashForm();
            sf.Show();
            var configPath = Rail.Commons.Paths.ApplicationConfigDir + "/RegisterCommandOFCommand";
            string line;

            foreach (var item in Directory.EnumerateFiles(Commons.Paths.ApplicationExePath))
            {
                sf.ShowMsg("正在加载插件" + item);
                Application.DoEvents();
                //System.Threading.Thread.Sleep(50);
            }

            using (System.IO.StreamReader sr = new System.IO.StreamReader(configPath))
            {
                // 从文件读取并显示行，直到文件的末尾 
                while (!string.IsNullOrEmpty((line = sr.ReadLine())))
                {
                    var assemblyPath = Rail.Commons.Paths.ApplicationBinDir + "/" + line;
                    if (!File.Exists(assemblyPath)) continue;
                    var assembly = Assembly.LoadFrom(assemblyPath);
                    sf.ShowMsg("正在加载插件" + assemblyPath);
                    Application.DoEvents();
                    System.Threading.Thread.Sleep(100);
                    var types = assembly.GetTypes();
                    for (var i = 0; i < types.Length; i++)
                    {
                        if (types[i].IsSubclassOf(typeof(SimpleCommand)))
                        {
                            RegisterCommand(types[i].Name, types[i]);
                        }
                    }
                }
            };
            sf.ShowMsg("正在启动....");
            Application.DoEvents();
            System.Threading.Thread.Sleep(500);
            sf.Close();
        }
        public void Startup(FormMain app)
        {
            SendNotification(STARTUP, app);
            var mm = new MainFormMediator(app);
            RegisterMediator(mm);
        }
    }
}
